package com.example.home.attorney.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.attorney.bean.ScoreBean;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class ScoreActivity extends AppCompatActivity {

    private String meAttorId;
    private ImageView back;
    private RatingBar ratingBar;
    private EditText etReview;
    private Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        Intent intent = getIntent();
        meAttorId = intent.getStringExtra("meAttorId");
        Log.e("xhfid",meAttorId);

        initView();
        scoreMethod();
    }

    private void scoreMethod() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取评分
                float rating = ratingBar.getRating();

                // 将浮点数评分四舍五入为整数
                Integer roundedRating = Math.round(rating);

                // 获取评价文本
                String reviewText = etReview.getText().toString();

                // 处理提交评价的逻辑，可以发送到服务器或进行其他操作
                // 在这里简单地显示整数评分和评价文本
                String evaluate = etReview.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id", meAttorId);
                    jsonObject.put("evaluate", evaluate);
                    jsonObject.put("score", roundedRating); // 使用四舍五入后的整数评分
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (!TextUtils.isEmpty(evaluate)){
                    OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice/evaluate",
                            OKHttpTool.POST,
                            ScoreBean.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    ScoreBean scoreBean = (ScoreBean) obj;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (scoreBean.getCode() == 200) {
                                                Toast.makeText(ScoreActivity.this, "评分: " + rating + "\n评价: " + reviewText, Toast.LENGTH_SHORT).show();
                                                ScoreActivity.this.finish();
                                            } else if (scoreBean.getCode() == 500) {
                                                Toast.makeText(ScoreActivity.this, "对不起，该咨询还没完成，无法评价", Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }


                                @Override
                                public void onFailure(String msg) {
                                    // 处理请求失败的情况
                                    Log.e("xhf","msg="+msg);
                                }
                            },jsonObject.toString());
                }
            }
        });

    }

    private void initView() {
        back = findViewById(R.id.back);
        ratingBar = findViewById(R.id.ratingBar);
        etReview = findViewById(R.id.etReview);
        btnSubmit = findViewById(R.id.btnSubmit);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScoreActivity.this.finish();
            }
        });
    }
}